import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
console.log(1111,process.env.VUE_APP_BASE_API);
const $axios = axios.create({
    baseURL: process.env.VUE_APP_BASE_API, 
    // withCredentials: true, // 跨域请求时发送cookie
    timeout: 5000 // 请求超时时间
  })

  // 拦截器
  $axios.interceptors.request.use(
    config => {
      if (store.getters.token) {
        config.headers['Token'] = getToken()
      }
      return config
    },
    error => {
      console.log(error) // for debug
      return Promise.reject(error)
    }
  )

  $axios.interceptors.response.use(
    response => {
      const res = response.data
  
      // if the custom code is not 20000, it is judged as an error.
      if (res.code !== 20000) {
        Message({
          message: res.message || 'Error',
          type: 'error',
          duration: 3000
        })
        // token过期处理
        if (res.code === 50008) {
          // to re-login
          MessageBox.confirm('登陆失效,请重新登录 !', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            //  token过期的操作 
          })
        }
        return Promise.reject(new Error(res.message || 'Error'))
      } else {
        return res
      }
    },
    error => {
      console.log(error) 
      Message({
        message: error.message,
        type: 'error',
        duration: 3000
      })
      return Promise.reject(error)
    }
  )

  export default $axios
  